package com.zkingsoft.rhi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkingsoft.rhi.entity.ImmovableHistoricalRelics;
import com.zkingsoft.rhi.entity.ImmovableHistoricalRelicsPublication;
import com.zkingsoft.rhi.handler.Result;
import com.zkingsoft.rhi.mapper.ImmovableHistoricalRelicsMapper;
import com.zkingsoft.rhi.service.ImmovableHistoricalRelicsNatureService;
import com.zkingsoft.rhi.service.ImmovableHistoricalRelicsPublicationService;
import com.zkingsoft.rhi.service.ImmovableHistoricalRelicsService;
import com.zkingsoft.rhi.service.ImmovableHistoricalRelicsTypeService;
import com.zkingsoft.rhi.utils.MyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 不可移动文物表 前端控制器
 * </p>
 *
 * @author xieguo
 * @since 2020-11-04
 */
@Api(tags = "不可移动文物")
@RestController
@RequestMapping("/rhi/immovable")
public class ImmovableHistoricalRelicsController {

    @Autowired
    ImmovableHistoricalRelicsService ihrs;

    @Autowired
    MyUtils myUtils;

    @Autowired
    ImmovableHistoricalRelicsMapper ihrm;

    @Autowired
    ImmovableHistoricalRelicsPublicationService ihrps;

    /**
     * @param jo
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：新增不可移动文物
     * @date 2020/11/10 11:15
     */
    @ApiOperation(value = "新增不可移动文物")
    @PostMapping("/insert")
    public JSONObject insertImmovableRelics(@RequestBody JSONObject jo) {
        JSONObject ihrJson = jo.getJSONObject("ihr");
        Integer publicationId = jo.getInteger("publicationId");
        ImmovableHistoricalRelics ihr = ihrJson.toJavaObject(ImmovableHistoricalRelics.class);
//        System.out.println("ihr=========================>"+ihr);
//        System.out.println("publicationId===============>"+publicationId);
        if (null==ihr.getNumber()){
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"参数错误");
        }

        if (ObjectUtil.isEmpty(getList(ihr))){
            ImmovableHistoricalRelicsPublication ihrp = ihrps.getById(publicationId);
            ihr.setLevel(ihrp.getLevel());
            ihr.setPublicationBatch(ihrp.getPublicationBatch());
            ihr.setPublicationNumber(ihrp.getPublicationNumber());
            ihr.setPublicationTime(ihrp.getPublicationTime());
            boolean save = ihrs.save(ihr);
            if (save) {
                return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS, "保存成功");
            } else {
                return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "保存失败");
            }
        }else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"文物编码不能重复");
        }
    }


    /**
     * @param jo
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：更新不可移动文物
     * @date 2020/11/10 11:16
     */
    @ApiOperation(value = "更新不可移动文物")
    @PostMapping("/update")
    public JSONObject updateImmovableRelics(@RequestBody JSONObject jo) {
        JSONObject ihrJson = jo.getJSONObject("ihr");
        Integer publicationId = jo.getInteger("publicationId");
        ImmovableHistoricalRelics ihr = ihrJson.toJavaObject(ImmovableHistoricalRelics.class);

        if (null==ihr.getNumber()){
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"参数错误");
        }

        ImmovableHistoricalRelics oldIhr = ihrs.getById(ihr.getId());

        if (!ihr.getNumber().equals(oldIhr.getNumber())){
            QueryWrapper<ImmovableHistoricalRelics> ihrqw = new QueryWrapper<>();
            ihrqw.eq("number",ihr.getNumber());
            List<ImmovableHistoricalRelics> list = ihrs.list(ihrqw);
            if (ObjectUtil.isNotEmpty(list)){
                return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"文物编码不能重复");
            }
        }

        ImmovableHistoricalRelicsPublication ihrp = ihrps.getById(publicationId);

        ihr.setLevel(ihrp.getLevel());
        ihr.setPublicationNumber(ihrp.getPublicationNumber());
        ihr.setPublicationBatch(ihrp.getPublicationBatch());
        ihr.setPublicationTime(ihrp.getPublicationTime());

        boolean update = ihrs.updateById(ihr);
        if (update) {
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS, "更新成功");
        } else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "更新失败");
        }
    }

    public List<ImmovableHistoricalRelics> getList(ImmovableHistoricalRelics ihr){
        QueryWrapper<ImmovableHistoricalRelics> ihrqw = new QueryWrapper<>();
        ihrqw.eq("number",ihr.getNumber());
        return ihrs.list(ihrqw);
    }

    /**
     * @param ihr
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：查询搜索不可移动文物
     * @date 2020/11/10 11:16
     */
    @ApiOperation(value = "查询搜索不可移动文物")
    @PostMapping("/query")
    public JSONObject queryImmovableRelics(@RequestBody JSONObject ihr) {
        return ihrs.queryImmovableRelics(ihr);
    }

    /**
     * @param ids
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：删除不可移动文物，可批量删除
     * @date 2020/11/10 11:17
     */
    @ApiOperation(value = "删除不可移动文物", notes = "可批量删除")
    @GetMapping("/delete/{ids}")
    public JSONObject deleteImmovableRelics(@PathVariable("ids") String ids) {
        List<Integer> serverIds = myUtils.handleIds(ids);
        boolean delete = ihrs.removeByIds(serverIds);
        if (delete) {
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS, "删除成功");
        } else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "删除失败");
        }

    }

    /**
     * @author xieguo
     * 功能描述：查询数据库字段是否为空
     * @param jo
     * @return com.alibaba.fastjson.JSONObject
     * @date 2020/11/18 10:17
     */
    @ApiOperation(value = "查询数据库字段是否为空",notes = "")
    @PostMapping("/query/typeIsNull")
    public JSONObject queryTypeIsNull(@RequestBody JSONObject jo){
        Integer offset = (Integer) jo.get("offset");
        Integer limit = (Integer) jo.get("limit");
        String param = (String) jo.get("param");
        String level = jo.getString("level");

        if (null==param){
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"参数错误");
        }
        if (null!=offset&&null!=limit&&offset>0&&limit>0){
            Page<ImmovableHistoricalRelics> ihrPage = new Page<>(offset, limit);
            QueryWrapper<ImmovableHistoricalRelics> wrapper = new QueryWrapper<>();

            switch (param){
                case "type":
                    wrapper.isNull("type").or().eq("type","");
                    break;

                case "nature":
                    wrapper.isNull("nature").or().eq("nature","");
                    break;

                case "publication":
                    wrapper.isNull("publication_time").or().isNull("publication_number")
                    .or().isNull("publication_batch").or().isNull("level")
                    .or().eq("publication_time","").or().eq("publication_number","")
                    .or().eq("publication_batch","").or().eq("publication_number","");
                    break;
            }

            if (StringUtils.isNotBlank(level)){
                wrapper.eq("level",level);
            }

            IPage<ImmovableHistoricalRelics> page = ihrs.page(ihrPage, wrapper);
            Integer total = (int) page.getTotal();
            return Result.setResultByCodeAndMsgAndDate(Result.STATUS_SUCCESS,"查询成功",page.getRecords(),total);

        }else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"参数错误");
        }
    }

    /**
     * @author xieguo
     * 功能描述：批量更新为空值的字段
     * @param jo
     * @return com.alibaba.fastjson.JSONObject
     * @date 2020/11/18 10:18
     */
    @ApiOperation(value = "批量更新为空值的字段")
    @PostMapping("/update/fieldIsNull")
    public JSONObject updateFieldIsNull(@RequestBody JSONObject jo){
        JSONObject ihrJson = jo.getJSONObject("ihr");
        ImmovableHistoricalRelics ihr = ihrJson.toJavaObject(ImmovableHistoricalRelics.class);
        String ids = jo.getString("ids");

        if (StringUtils.isNotBlank(ihr.getPublicationNumber())){
            Integer publicationId = new Integer(ihr.getPublicationNumber());
            ImmovableHistoricalRelicsPublication ihrp = ihrps.getById(publicationId);
            ihr.setPublicationNumber(ihrp.getPublicationNumber());
            ihr.setPublicationBatch(ihrp.getPublicationBatch());
            ihr.setPublicationTime(ihrp.getPublicationTime());
            ihr.setLevel(ihrp.getLevel());
        }

        List<Integer> idsList = myUtils.handleIds(ids);
        int count = ihrm.batchUpdateFieldIsNull(ihr, idsList);
        if (count>0){
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS,"更新成功");
        }else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"更新失败");
        }
    }

    @ApiOperation(value = "回显")
    @GetMapping("/selectImmovableByIds/{id}")
    public JSONObject selectUnitById(@PathVariable String id) {
        ImmovableHistoricalRelics ihr = ihrs.getById(id);
        QueryWrapper<ImmovableHistoricalRelicsPublication> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(ihr.getPublicationNumber())){
            wrapper.eq("publication_number",ihr.getPublicationNumber());
        }
        ImmovableHistoricalRelicsPublication ihrp = ihrps.getOne(wrapper);
        if (ObjectUtil.isEmpty(ihrp)&&null==ihrp.getId()){
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"没有相对应的公布批次数据");
        }
        ihr.setPublicationNumber(ihrp.getId().toString());
        return Result.setResultByCodeAndMsgAndDate(Result.STATUS_SUCCESS, "查询成功", ihr, null);
    }

    @ApiOperation(value = "删除所有表数据")
    @GetMapping("/delete/all")
    public JSONObject deleteAll(){
        int delete = ihrm.deleteAll();
        if (delete>0){
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS,"已清空所有数据");
        }else {
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS,"数据库里面没有数据");
        }
    }
}

